Ik lijst met contacten van Web Service en geef het weer in de 'gesegmenteerde' tabelweergave van contacten zoals te zien in de schermafbeelding. Het probleem is dat ik dezelfde tag-waarden krijg voor selectievakjes van de eerste rij voor sectie A en sectie S. Ik heb één array gesorteerd en weergegeven in de geïndexeerde tabelweergave. Hoe u verschillende tagwaarden kunt krijgen, afhankelijk van indexPath.row, ongeacht het aantal weergegeven secties. Dit is wat ik heb geprobeerd In cellForRowAtIndexPath: UIButton * checkBox; if (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiomPhone) { checkBox = [[UIButton alloc] initWithFrame: CGRectMake (7, 8, 30, 30)]; } anders { checkBox = [[UIButton alloc] initWithFrame: CGRectMake (15, 13, 30, 30)]; } // int cnt = 0; // for (int i = indexPath.section - 1; i> 0; i--) // { // cnt + = [[objectsForCharacters objectForKey: [arrayOfCharacters objectAtIndex: i]] count]; // arrayOfCharachters heeft char 'A' tot 'Z' //} //checkBox.tag = cnt + indexPath.row; [checkBox setImage: [UIImage imageNamed: @ "checkBox.png"] forState: UIControlStateNormal]; [checkBox addTarget: zelfactie: @selector (checkBoxClicked :) forControlEvents: UIControlEventTouchUpInside]; [checkBox setTag: indexPath.row]; [cell.contentView addSubview: checkBox]; terugkeer cel; } - (void) checkBoxClicked: (id) afzender { CGPoint buttonPosition = [afzender convertPoint: CGPointZero toView: self.tableViewContact]; NSIndexPath * indexPath = [self.tableViewContact indexPathForRowAtPoint: buttonPosition]; UIButton * tappedButton = (UIButton *) afzender; NSLog (@ "Tag-nummer =% d", [afzender-tag]); if ([tappedButton.currentImage isEqual: [UIImage imageNamed: @ "checkBox.png"]]) { [afzender setImage: [UIImage imageNamed: @ "checkBoxMarked.png"] forState: UIControlStateNormal]; if (indexPath! = Nil) { NSString * finalIntId = [mutableArrayOfIds objectAtIndex: indexPath.row]; // store check box-id's in mutableArrayOfIds NSLog (@ "Getagde aangevinkte knop id =% @", finalIntId); [arrayOfIds addObject: finalIntId]; } // NSString * finalIntId = [mutableArrayOfIds objectAtIndex: tappedButton.tag]; // NSString * finalIntId = [mutableArrayOfIds objectAtIndex: indexPath.row]; } anders { [afzender setImage: [UIImage imageNamed: @ "checkBox.png"] forState: UIControlStateNormal]; NSLog (@ "UnChecked"); // [arrayOfIds removeObjectAtIndex: tappedButton.tag]; } } - (NSString *) tableView: (UITableView *) tableView titleForHeaderInSection: (NSInteger) sectie { if ([arrayOfCharacters count] == 0) { return @ ""; } retourneer [NSString stringWithFormat: @ "% @", [arrayOfCharacters objectAtIndex: sectie]]; } - (NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView { NSArray * toBeReturned = [NSArray arrayWithArray: [@ "A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | # " componentsSeparatedByString: @ "|"]]; terug naarBeReturned; } - (NSInteger) tableView: (UITableView *) tableView sectionForSectionIndexTitle: (NSString *) titel atIndex: (NSInteger) index { NSGeheel getal = 0; voor (NSString * teken in arrayOfCharacters) { if ([character isEqualToString: title]) { terug tellen; } tel ++; } retourneer 0; } - (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView { retourneer [arrayOfCharacters count]; // retourneer 1; } - (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) sectie { // return [mutableArray count]; retourneer [[objectsForCharacters objectForKey: [arrayOfCharacters objectAtIndex: sectie]] aantal]; }
2020-12-07 22:54:13
u stelt dezelfde tagwaarde in voor alle secties die dezelfde rij hebben. Het indexPath heeft twee eigenschappen, {section, row}. Laten we zeggen dat sectie A twee rijen heeft, voor rij1 -> indexPath.section = 0, indexPath.row = 0; voor rij2-> indexPath.section = 0, indexPath.row = 1; Laten we zeggen dat sectie S twee rijen heeft, voor rij1 -> indexPath.section = 1, indexPath.row = 0; voor rij2-> indexPath.section = 1, indexPath.row = 1; Dus voor rij1 van sectie A en rij1 van sectie S stelt u dezelfde tagwaarde in die 0 is. Er is uw probleem. Probeer de tagwaarde in te stellen zoals hieronder. button.tag = indexPath.section * 1000 + indexPath.row; bij het ophalen van de sectie en rij, NSInteger sectie = (button.tag) / 1000; NSInteger rij = (button.tag)% 1000; | Probeer dit... - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { statische NSString * CellIdentifier = @ "Cell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; if (cel == nihil) { cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier]; } UIButton * checkBox = [[UIButton alloc] initWithFrame: CGRectMake (280, 10, 50, 44)]; checkBox.backgroundColor = [UIColor orangeColor]; [checkBox addTarget: zelfactie: @selector (checkBoxClicked: event :) forControlEvents: UIControlEventTouchUpInside]; [checkBox setTag: indexPath.row]; [cell.contentView addSubview: checkBox]; terugkeer cel; } - (void) checkBoxClicked: (id) afzendergebeurtenis: (id) gebeurtenis { NSSet * touches = [event allTouches]; UITouch * touch = [raakt anyObject aan]; CGPoint currentTouchPosition = [touch locationInView: self.tv]; // hier is tv TableView Object NSIndexPath * indexPath = [self.tv indexPathForRowAtPoint: currentTouchPosition]; NSLog (@ "waarde van indePath.section% d, indexPath.row% d", indexPath.section, indexPath.row); } | Dit gebeurt omdat u tags toewijst aan knoppen ONAFHANKELIJK van secties. Beide eerste rij van secties A en S hebben rij = 0. dus de tag die is toegewezen aan hun respectieve knop is 0. U moet ze toewijzen met behoud van verwijzing naar uw secties. Ik zou willen voorstellen om toegankelijkheidstips toe te wijzen met een door komma's gescheiden formulier dat Sectie, Rij bevat. En in uw methode - (void) checkBoxClicked: (id) afzender { // kies een string uit een door komma's gescheiden vorm. 1e is uw sectie, 2e is rij. } De tweede optie is Doe wat je ook doet en implementeer je Button-methode op deze manier. CGPoint buttonPosition = [afzender convertPoint: CGPointZero toView: self.tableView]; NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: buttonPosition]; if (indexPath! = nil) { ... indexpath.section is jouw sectie, index path.row is jouw rij. } Er is ook een derde optie. in cellforRowAtIndexpath uw Button een titel toewijzen - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath [btn setTitle: <# Uw rij #> forState: UIControlStateDisabled]; [btn setTag <#Uw sectie #>]; dus bij ontvangst in uw knopmethode kunt u zowel sectie (tag) als rij (titel voor uitgeschakelde status) hebben. - (void) checkBoxClicked: (id) afzender {[button titleForState: UIControlStateDisabled]; // jouw rij button.tag // uw sectie} | Probeer dit! Elke sectie kent u misschien het aantal secties rechts, en voegt vervolgens de telling van de individuele rij toe. [[fieldTitlesList objectAtIndex: indexPath.section - 1] count] + indexPath.row Waar fieldTitlesList de array van uw secties is. | Ik heb de volgende code toegevoegd die mijn probleem heeft opgelost NSInteger rowNumber = 0; voor (NSInteger i = 0; i